ActiveAdmin.register Conversion do
  menu label: '转换率', parent: '数据统计'
  filter :company, label: '集团'

  actions :index
  controller do
    def index
      @company = params[:q][:company_contains] rescue ""
      index! do |format|
        format.html {render 'admin/conversions/index', layout: 'active_admin'}
      end
    end

    def data
      render json: {
          total: Conversion.ransack(company_contains: params[:company]).result.count,
          rows: Conversion.ransack(company_contains: params[:company]).result.order(company: :desc, report_date: :desc).map {|r|
            {
                report_date: r.report_date,
                company: r.company,
                avg: r.avg
            }
          }
      }
    end
  end
end
